package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectAndPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectAndPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.service.NursingProjectAndPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectAndPlanMapper npapMapper;




    @Autowired
    private NursingProjectAndPlanService npapService;

    /**
     * 分页
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse selectPlanByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page1 = nursingPlanMapper.selectPlanByPage(name, status);
        return PageResponse.of(page1, NursingPlanVo.class);
    }


    /**
     * 添加护理计划
     *
     * @param nursingPlanDto
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void addPlan(NursingPlanDto nursingPlanDto) {
        //重名校验
        if (nursingPlanMapper.getByName(nursingPlanDto.getPlanName()) != null) {
            throw new BaseException(BasicEnum.NURSING_PLAN_INSERT_FAIL_CODE);
        }

        nursingPlanDto.setStatus(1);
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.addPlan(nursingPlan);


////        nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(nursingPlan.getId()));
////        npapMapper.addList(nursingPlanDto.getProjectPlans());
           List<NursingProjectAndPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        if (nursingPlanDto.getProjectPlans() != null && nursingPlanDto.getProjectPlans().size() > 0) {
           projectPlans.forEach(v -> v.setPlanId(nursingPlan.getId()));
       }
        npapService.addList(projectPlans);


//        if (nursingPlanDto.getProjectPlans() != null ) { //|| nursingPlanDto.getProjectPlans().size() > 0
//            for (NursingProjectAndPlanDto projectPlan : nursingPlanDto.getProjectPlans()) {
//                projectPlan.setPlanId(nursingPlanDto.getId());
//
//            }
//            nursingPlanService.addpj(nursingPlanDto.getProjectPlans());
//        }
//
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAll() {


        return    nursingPlanMapper.getAll();
    }


    /**
     * 根据id查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {

       return nursingPlanMapper.getPlanById(id);


    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto
     * @return
     *
     */
    @Transactional
    @Override
    public void updatePlanById(NursingPlanDto nursingPlanDto) {

        if (nursingPlanMapper.getByName1(nursingPlanDto.getPlanName(),nursingPlanDto.getId()) != null) {
            throw new BaseException(BasicEnum.NURSING_PLAN_INSERT_FAIL_CODE);
        }


        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
       //根据id删除已有护理项目
        npapMapper.deleteByPlanId(nursingPlanDto.getId());

        List<NursingProjectAndPlanDto> projectPlans = nursingPlanDto.getProjectPlans();

        if (projectPlans != null && projectPlans.size() > 0) {
            for (NursingProjectAndPlanDto projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlanDto.getId());
            }
            npapService.addList(projectPlans);
        }

        nursingPlanMapper.updatePlanById(nursingPlan);

    }


    /**
     * 删除护理计划
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public void removeById(Long id) {
        //判断删除的时候，护理等级是否用到护理计划
//         if (!nursingPlanMapper.){
//             throw new BaseException(BasicEnum.NURSING_PLAN_DELETE_FAIL_CODE);
//         }
//        nursingPlanMapper.removemsgById()


        nursingPlanMapper.removeById(id);

        nursingPlanMapper.removepj(id);

    }


    /**
     * 修改状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateStatus(Long id, Integer status) {

        return nursingPlanMapper.updateStatus(id, status);
    }


}

